<?php
namespace admin\action;

use api\utils\SaasApiClient;
use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;
use herosphp\core\Log;
use herosphp\core\Loader;
use utils\sms\GetCr6868Info;
Loader::import('utils.sms.GetCr6868Info');

/**--tmpl-use--**/

class SettingAction extends CommonAction {
	private	$settingItems = array(
		//基本设置 
		'site_name' ,
		'site_url',
		'site_logo',
		'site_logo2',

		'site_icp',
		'site_copyright',
		'title_sperator',

		'enabled_register',
		'register_contact',
		'site_enabled',
		'site_down_page',
		'statistic_code',
		//商务合作
		'cs_telephone',
		'cs_email' ,
		'business_qq',

		//客服设置
		'cs_work_time',
		'cs_phone',

		'cs_wx_public',
		'wx_qr',
		'cs_qq',
		//上传设置
		'file_save_path',
		'max_file_size',
		'allow_file_ext',

		'image_save_path',
		'max_image_size',
		'allow_image_ext',

		'media_save_path',
		'max_media_size',
		'allow_media_ext',
		//SEO设置
		'title' ,
		'keywords',
		'description',
		'index_name',
		'html_ext',
		'enabled_index_cache',
		'enabled_url_rewrite',
		//交易设置
		'order_life', //订单生存时间
		'pro_unit', //商品单位
		'pro_std', //商品规格
		'order_mode',//交易模式
		//短信设置
		'sms_open', //短信服务　开启／关闭
		'sms_provider', //短信服务商 短信宝／云片网络
		'sms_account', //短信平台账号
		'sms_password', //短信平台密码
		'sms_key', //短信平台KEY　(非必填项，使云片网络时可用到
		'sms_sign', //短信内容签名
		'sms_test_content', //测试短信
		'sms_risk', //短信警告数量
		//合同设置
		'contract_phone',//卖家联系电话
		'contract_fax', //卖家传真


	);
	

	public function C_start()
	{
		parent::C_start();	
		/**--tmpl-starttodo--**/
	}

	/**--tmpl-vars--**/

	protected $serviceBean="admin.adminSetting.service";
	
	public function index(HttpRequest $request) {
		$service = Beans::get($this->getServiceBean());
		$configs = $service->getSettings($this->settingItems);
		if($configs['cs_qq']=='[]'){
			$configs['cs_qq']='[{"name":"","qq":""}]';
		}
		$configs['cs_qq'] = json_decode($configs['cs_qq'],true);

		$configs['cs_work_time'] = json_decode($configs['cs_work_time'],true);

        $user_info = $this->__getUserInfo('virtual_account,sign_bank_branch');
        $company_info = $this->__getCompanyInfo('company,address,prov,city,dist');
        $ids[] = $company_info['prov'];
        $ids[] = $company_info['city'];
        $ids[] = $company_info['dist'];
        $url = "api/area/getAreaByids";
        $result = SaasApiClient::send($url,array('ids'=>$ids));
        $area = '';
        if($result->isSucess()){
            $area = $result->getItem();
        }else{
            Log::error($result -> getMessage());
        }

		$configs['sms_risk'] = $configs['sms_risk']>0?$configs['sms_risk']:100;
		$smsService = new GetCr6868Info();
		$smsService->set($configs['sms_account'],$configs['sms_password']);
		$smsInfo = $smsService->getInfo();
		$this->assign('smsBalance',$smsInfo['balance']);

        $company_info['address'] = $area.$company_info['address'];
		$this->assign('configs',$configs);
		$this->assign('user_info',$user_info);
		$this->assign('company_info',$company_info);
		$this->assign('site_logo_id',$service->getConfigId('site_logo'));
		$this->assign('site_wxqr_id',$service->getConfigId('wx_qr'));
		$this->assign("title", "站点设置");
		$this->assign('bread', array('网站设置',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;','javascript:;'));
		$this->setView('setting/index');
	}
	
	/**-- doEditMethod --**/
	public function do_edit(HttpRequest $request){
		$data = $request->getParameter('configs');
        if(isset($data['sms_open'])){
			$data['sms_open'] = $data['sms_open'] ? 'on' : 'off';
		}
		if(isset($data['pro_unit'])){
			$data['pro_unit'] = $data['pro_unit'] ? $data['pro_unit'] : '吨';
		}
		if(isset($data['pro_std'])){
			$data['pro_std'] = $data['pro_std'] ? $data['pro_std'] : 'KG/袋';
		}
		if(isset($data['enabled_register'])) {

			$data['enabled_register'] = $data['enabled_register'] ? 'on' : 'off';
		}
		if(isset($data['enabled_index_cache'])) {

			$data['enabled_index_cache'] = $data['enabled_index_cache'] ? 'on' : 'off';
		}
		if(isset($data['site_enabled'])) {

			$data['site_enabled'] = $data['site_enabled'] ? 'on' : 'off';
		}

		$qq_names = $request->getParameter('name');
		$qq_qq = $request->getParameter('qq');
		$startday = $request->getParameter('startday');
		$endday = $request->getParameter('endday');
		$starttime = $request->getParameter('starttime');
		$endtime = $request->getParameter('endtime');
		$worktime_enabled = $request->getParameter('worktime_enabled');
		$worktime_arr = array();

		for($i = 0; $i < 2; $i ++){
			$tmp = array();
			$tmp['startday'] = $startday[$i];
			$tmp['endday'] = $endday[$i];
			$tmp['starttime'] = $starttime[$i];
			$tmp['endtime'] = $endtime[$i];
			$tmp['worktime_enabled'] = $worktime_enabled[$i];
			$worktime_arr[] = $tmp;
		}
		
		$qq_arr = array();
		foreach($qq_names as $k => $v)
		{
			if($v && $qq_qq[$k])
			{
				$tmp = array();
				$tmp['name'] = $v;
				$tmp['qq'] = $qq_qq[$k];
				$qq_arr[] =  $tmp;
			}
		}

		$data['cs_qq'] = addslashes(json_encode($qq_arr));
		$data['cs_work_time'] = json_encode($worktime_arr);

		$service = Beans::get($this->getServiceBean());
		if($service->setConfigs($data)){
			AjaxResult::ajaxSuccessResult();
		}
		AjaxResult::ajaxFailtureResult();
	}
	
	

}
